package com.lovestacks.tj.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lovestacks.tj.bean.PersonInfomation;
import com.lovestacks.tj.bean.ResultBean;
import com.lovestacks.tj.service.RegisterService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/personInfo")
public class PersonInfoController {

    @Autowired
    private RegisterService registerService;
    @RequestMapping("register")
    @ResponseBody
    public ResultBean register(String openId,String name,String phone,String depart,String menu){
           ResultBean bean = new ResultBean();
           try{
               registerService.insertRegisterInfo(openId ,name, phone, depart, menu);
               Map<String,Object> map = new HashMap<>();
               map.put("msg","插入或更新成功");
               bean.makeSuccess(map);
           }catch (Exception e){
               e.fillInStackTrace();
               bean.makeFail(e.getMessage());
           }
           return bean;
    }

    @RequestMapping("getInfo")
    @ResponseBody
    public ResultBean getInfo(String id){
        ResultBean bean = new ResultBean();
        try{
            PersonInfomation result = registerService.getInfo(id);
            Map<String,Object> map = new HashMap<>();
            map.put("personInfo",result);
            bean.makeSuccess(map);
        }catch (Exception e){
            e.fillInStackTrace();
            bean.makeFail(e.getMessage());
        }
        return bean;
    }

    @RequestMapping("getOpenid")
    @ResponseBody
    public Map<String,Object> getOpenId(String code){
        String appid = "wx712e8407b8e2396d";
        String secret = "ea500dd7368c4fef94ce1163f5a03921";
        Map<String,Object> resultMap = new HashMap<>();
        try {
            URL url = new URL("https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            if (conn.getResponseCode() == 200){
                String str = IOUtils.toString(conn.getInputStream(), "utf-8");
                JSONObject jsonObject = JSON.parseObject(str.trim());
                if (jsonObject.containsKey("openid")){
                    resultMap.put("status",200);
                    resultMap.put("message","结果返回成功");
                    resultMap.put("data",jsonObject);
                }else {
                    resultMap.put("status",400);
                    resultMap.put("message","code已经使用过了");
                    resultMap.put("data",jsonObject);
                }
            }
            else{
                resultMap.put("status",401);
                resultMap.put("message","连接获取失败");
                resultMap.put("data",new JSONObject());
            }
        } catch (Exception e) {
            resultMap.put("status",402);
            resultMap.put("message","程序报错"+e.getMessage());
            resultMap.put("data",new JSONObject());
        }
        return resultMap;
    }
}
